/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/*angular.module('activitiModeler').controller('KisBpmFormKeyDisplayCtrl',
    [ '$scope', '$modal', '$http', function($scope, $modal, $http) {
    
    if ($scope.property && $scope.property.value && $scope.property.value.id) {
   		$http.get(ACTIVITI.CONFIG.contextRoot + '/app/rest/models/' + $scope.property.value.id)
            .success(
                function(response) {
                    $scope.form = {
                    	id: response.id,
                    	name: response.name
                    };
                });
    }
	
}]);*/

angular.module('activitiModeler').controller('KisBpmFormKeyCrtl',
    [ '$scope', '$modal', '$http', function($scope, $modal, $http) {
	
     // Config for the modal window
     var opts = {
         template:  'editor-app/configuration/properties/form-key-popup.html?version=' + Date.now(),
         scope: $scope
     };

     // Open the dialog
     _internalCreateModal(opts, $modal, $scope);
}]);

angular.module('activitiModeler').controller('KisBpmFormKeyPopupCrtl',
    [ '$rootScope', '$scope', '$http', '$location', function($rootScope, $scope, $http, $location) {
	 
	$scope.state = {'loadingForms' : true, 'formError' : false};
	
	$scope.popup = {'state' : 'formReference'};
    
    $scope.foldersBreadCrumbs = [];
    
    // Close button handler
    $scope.close = function() {
    	$scope.property.mode = 'read';
        $scope.$hide();
    };
    
    // Selecting/deselecting a subprocess
    $scope.selectForm = function(form, $event) {
   	 	$event.stopPropagation();
   	 	if ($scope.selectedForm && $scope.selectedForm.id && form.id == $scope.selectedForm.id) {
   	 		// un-select the current selection
   	 		$scope.selectedForm = null;
   	 	} else {
   	 		$scope.selectedForm = form;
   	 	}
    };
    
    // Saving the selected value
    $scope.save = function() {
   	 	
   	 	var formCheck =document.getElementsByName("formCkeck");
   	    var  check_val = [];
         for(k in formCheck){
           if(formCheck[k].checked)
             check_val.push(formCheck[k].value);
          }
         var fromTrue = check_val.join(",")
   	    $scope.property.value=fromTrue;
   	 	$scope.updatePropertyInModel($scope.property);
   	 	$scope.close();
    };
    //search
    $scope.search = function() {
   	 	if($scope.formKey==undefined){
   	 	$scope.formKey="";
   	 	};
   	 	if($scope.formName==undefined){
   	 	$scope.formName="";
   	 	};
    	$http({ method: 'GET', url: ACTIVITI.CONFIG.contextRoot + "/form/getFormTemplateList?" + "name="+$scope.formName+"&keyVal="+$scope.formKey })
        .success(function(data) {
        	console.log(data)
        	$scope.formName = "";
        	$scope.formKey = "";
        	$scope.templates=data;  
        }).error(function(err) {
            console.log(err)
        });

    };
   
    
    
    
    $scope.cancel = function() {
        $scope.close();
    };

   
}]);